package org.wattdepot.cli.command;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.resource.property.jaxb.Property;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Used to get all properties relating to a source at a specific time.
 * 
 * @author Kimberly Heu, Kendyll Doi
 */
public class WattSensorDataTime {
  /**
   * Gets all of the properties for a given source at a given time.
   * 
   * @param sourceName The name of the source.
   * @param time The time to get the data.
   * @param client the WattDepot client to use.
   * @return a string containing all information on the source at the time.
   */
  public static String wattSensorDataTime(String sourceName, String time, 
      WattDepotClient client) {
    // Messages to be displayed in the case of an error
    String resourceNotFoundMessage = "No data found for ";
    String miscClientMessage = "Error while retrieving resource";
    String notAuthorizedMessage = "Not authorized to retrieve information.";
    String badXmlMessage = "Error enountered getting information from server.";
    
    
    String output = null; // The output string
    StringBuffer buf = new StringBuffer(500); // Collects our output
    XMLGregorianCalendar timestamp; // Timestamp to check for data
    Source summarySource; // The source we are getting data on
    
    try {
      // Initalize the source
      summarySource = client.getSource(sourceName);
      
      // Try to create a timestamp. If it is invalid, the method will return null
      // so return the appropriate error message.
      timestamp = CommandLineInterface.createTstamp(time);
      if (timestamp == null) {
        return ("Incorrect timestamp input.");
      }
      
      // Get the information on the specific source
      SensorData sourceData = null;
      sourceData = client.getSensorData(sourceName, timestamp);
      
      // Format output
      buf.append("Tool: " + sourceData.getTool() + "\n Source: " + sourceData.getSource());
      buf.append("\n Properties: ");
      // Check if given source has properties at the given time.
      if (summarySource.isSetProperties()) {
        // Print every property, if any properties exist.
        for (Property property : sourceData.getProperties().getProperty()) {
          buf.append("(" + property.getKey() + " : " +  property.getValue() + ") ");
        }
      }
      else {
        // Source has no properties, so say so.
        buf.append("No properties");
      }
    }
    catch (ResourceNotFoundException e) {
      // Occurs if source does not exist
      buf.append(resourceNotFoundMessage);
      buf.append(sourceName);
    }
    catch (MiscClientException e) {
      // Occurs if error happens while getting data
      buf.append(miscClientMessage);
    }
    catch (NotAuthorizedException e) {
      // Occurs if user is not authorized to get information
      buf.append(notAuthorizedMessage);
    }
    catch (BadXmlException e) {
      // Occurs if data is corrupted
      buf.append(badXmlMessage);
    }
    catch (Exception e) {
      // Catch any other errors
      buf.append("Encountered error: ");
      buf.append(e);
    }
    
    // Give output variable information from buffer and return.
    output = buf.toString();
    return output;
  }
}
